from xmlrpc.client import MAXINT

from PyQt5.QtCore import QDateTime
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QDateTimeEdit, QComboBox, QLineEdit, QPushButton

from src.utils.WheelSpinBox import WheelSpinBox

class TimeConverterUI(QWidget):
    def __init__(self):
        super().__init__()
        self.hex_io = None
        self.byte_order = None
        self.seconds_input = None
        self.update_time_button = None
        self.end_time = None
        self.start_time = None
        self.init_ui()

    def init_ui(self):
        main_layout = QVBoxLayout()

        # 时间输入区域
        time_layout = QHBoxLayout()
        time_layout.addWidget(QLabel("起始时间:"))
        self.start_time = QDateTimeEdit()
        self.start_time.setDisplayFormat("yyyy/MM/dd HH:mm:ss")
        self.start_time.setDateTime(QDateTime(1970, 1, 1, 8, 0))
        time_layout.addWidget(self.start_time)

        time_layout.addWidget(QLabel("结束时间:"))
        self.end_time = QDateTimeEdit()
        self.end_time.setDisplayFormat("yyyy/MM/dd HH:mm:ss")
        self.end_time.setDateTime(QDateTime.currentDateTime())
        time_layout.addWidget(self.end_time)
        main_layout.addLayout(time_layout)

        #更新结束时间按钮
        self.update_time_button = QPushButton("时间校准")
        time_layout.addWidget(self.update_time_button)
        main_layout.addLayout(time_layout)

        # 秒数输入区域（带滚轮）
        seconds_layout = QHBoxLayout()
        seconds_layout.addWidget(QLabel("时间差(秒):"))
        self.seconds_input = WheelSpinBox()
        self.seconds_input.setRange(0, MAXINT)
        seconds_layout.addWidget(self.seconds_input)
        main_layout.addLayout(seconds_layout)

        # 字节序选择
        self.byte_order = QComboBox()
        self.byte_order.addItems(["大端序", "小端序"])
        order_layout = QHBoxLayout()
        order_layout.addWidget(QLabel("字节序:"))
        order_layout.addWidget(self.byte_order)
        main_layout.addLayout(order_layout)

        # 16进制输入区域
        hex_layout = QHBoxLayout()
        hex_layout.addWidget(QLabel("16进制:"))
        self.hex_io = QLineEdit()
        hex_layout.addWidget(self.hex_io)
        main_layout.addLayout(hex_layout)

        self.setLayout(main_layout)
        self.setWindowTitle("时间转换工具")
        self.resize(450, 200)